package com.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dao.dao.IGoodsDao;
import com.dao.dao.IOrderDao;
import com.dao.entity.Goods;
import com.dao.redis.IRedislLoginVerifyDao;
import com.net.base.BaseRequest;
import com.net.base.BaseResponse;
import com.net.filter.LoginFilter;
import com.util.NetUtil;
import com.util.Utils;

/**
 * 商家处理业务的controller
 * 
 * @author 尚业
 *
 */

@Controller
@RequestMapping(value = "/seller")
public class SellerController {
	private static Logger logger = Logger.getLogger(LoginFilter.class);

	@Autowired
	IGoodsDao goodsDao;
	@Autowired
	IRedislLoginVerifyDao redislLoginVerifyDao;
	@Autowired
	IOrderDao orderDao;

	/**
	 * 展示卖家商品，传入offset和limit
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/showGoods")
	@ResponseBody
	public BaseResponse<List<Goods>> showGoods(HttpServletRequest request,@RequestBody BaseRequest<Map<String, Integer>> requestBody) {
		logger.warn("显示货物");
		if (requestBody == null) {
			return NetUtil.getCommonFailuerResponse("请求数据为空");
		}
		Map<String, Integer> mapRequest = requestBody.getBody();
		if (mapRequest == null) {
			return NetUtil.getCommonFailuerResponse("请求数据为空");
		}
		String userName = Utils.getShopnameBytaken(redislLoginVerifyDao,
				request.getHeader("JSESSIONID"));
		if (userName == null) {
			return NetUtil.getCommonFailuerResponse("会话过期");
		}
		List<Goods> listResult = goodsDao.selectUserNameLimit(userName,
				mapRequest.get("offset"), mapRequest.get("limit"));
		return NetUtil.getDataResponse(listResult, "数据请求成功");
	}

	/**
	 * 通过id删除获取商品
	 * 
	 * @param request
	 * @param requestBody
	 * @return
	 */
	@RequestMapping(value = "/deleteGoodsByid")
	@ResponseBody
	public BaseResponse<Goods> deleteGoodsByid(HttpServletRequest request,
			@RequestBody BaseRequest<Goods> requestBody) {
		if (requestBody == null) {
			return NetUtil.getCommonFailuerResponse("请求数据为空");
		}

		Goods goods = NetUtil.getBodyFromBaseRequest(requestBody, Goods.class);
		if (goods == null) {
			return NetUtil.getCommonFailuerResponse("请求数据为空");
		}

		// 删除图片逻辑
		Goods goodsDbData = goodsDao.selectByPrimaryKey(goods.getGoodsid());
		String image = goodsDbData.getImage();
		if (image!=null&&!NetUtil.deleteFileByUrl(image)) {
			return NetUtil.getCommonFailuerResponse("删除物品图片失败");
		}
		return NetUtil.returnResponseByDbCode(goodsDao.deleteByPrimaryKey(goods
				.getGoodsid()));
	}

	/**
	 * 通过id修改商品忽略修改数据时为null的字段
	 * 
	 * @param request
	 * @param requestBody
	 * @return
	 */
	@RequestMapping(value = "/updateGoodsByid")
	@ResponseBody
	public BaseResponse<Goods> updateGoodsByid(HttpServletRequest request,
			BaseRequest<Goods> requestBody) {
		if (requestBody == null) {
			return NetUtil.getCommonFailuerResponse("请求数据为空");
		}
		Goods goods = NetUtil.getBodyFromBaseRequest(requestBody, Goods.class);
		if (goods == null) {
			return NetUtil.getCommonFailuerResponse("请求数据为空");
		}
		return NetUtil.returnResponseByDbCode(goodsDao
				.updateByPrimaryKeySelective(goods));
	}

	/**
	 * 卖家添加商品
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/addGoods")
	@ResponseBody
	public BaseResponse<Goods> addGoods(@RequestBody BaseRequest<Goods> request) {
		logger.warn("添加货物");
		if (request == null) {
			return NetUtil.getCommonFailuerResponse("请求数据为空");
		}
		Goods goods = NetUtil.getBodyFromBaseRequest(request, Goods.class);
		if (goods == null) {
			return NetUtil.getCommonFailuerResponse("请求数据为空");
		}
		goodsDao.insert(goods);
		return NetUtil.getCommonSuccessResponse("添加成功");
	}

}
